// pages/info/info.js
import {
  addRecordApi,
  uploadFacePy,
  base64ToFile
} from '../../public/api'
import Notify from '../../miniprogram/miniprogram_npm/@vant/weapp/notify/notify';
import {
  baseURL,
  PyBaseURL
} from "../../public/request";
import {
  setIdNumber,
  getIdNumber
} from "../../utils/util.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    minDate: new Date().getTime(),
    maxDate: new Date(new Date().getFullYear(), new Date().getMonth() + 5, new Date().getDate() + 5).getTime(),
    currentDate: new Date().getTime(),
    overlayShow: false,

    sex: '0',
    photo: "",
    name: "",
    phoneNumber: "",
    idCard: "",
    accessTime: "",
    isDeletel: '0',
    approve: '0'

  },

  onLoding() {
    this.setData({
      onLoding: true
    });
  },
  onUnLoding() {
    this.setData({
      onLoding: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${new Date().getFullYear()}-${date.getMonth() + 1 < 10 ? '0' : ''}${date.getMonth() + 1}-${date.getDate() < 10 ? '0' : ''}${date.getDate()} ${date.getHours()}:${date.getMinutes()}:00`;
  },

  onInput(event) {
    console.log(this.formatDate(event.detail));
    this.setData({
      accessTime: this.formatDate(event.detail),
    });
  },
  onSubmit() {
    //this.onLoding()
    console.log(this.data.photo)
    console.log(this.data.idCard)
    console.log(this.data.accessTime)
    if (this.data.photo == '' || this.data.idCard == '' || this.data.accessTime == '') {
      Notify({
        type: 'danger',
        message: "请填写必要信息(面部、身份证号以及访问时间)"
      });
    } else {
      setIdNumber(this.data.idCard)
      addRecordApi({
        sex: this.data.sex,
        photo: this.data.photo,
        name: this.data.name,
        phoneNumber: this.data.phoneNumber,
        idCard: this.data.idCard,
        accessTime: this.data.accessTime,
        isDeletel: this.data.isDeletel,
        approve: this.data.approve
      }).then(res => { 
        Notify({
          type: 'primary',
          message: res.data.msg
        });
      }).catch(err => {
        console.log('失败回调', err)
      })
      this.handleFile(this.data.photo)
    }

  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    const {
      fileList = []
    } = this.data.fileList;
    fileList.push({
      url: file.url,
      name: "",
      deletable: true,
    });
    this.setData({
      fileList
    });
    const _this = this; // 存储外部的 this
    console.log(file);
    wx.uploadFile({
      url: baseURL + '/common/upload',
      filePath: file.url,
      name: 'file',
      success(res) { 
        _this.setData({
          photo: JSON.parse(res.data).data
        }); 
      },
      fail(){
        Notify({
          type: 'danger',
          message: "网络异常"
        });
      }
    });
  },
  handleFile(file) {
    const _this = this; // 存储外部的 this 
    var formData = {
      uid: this.data.idCard,
      ugroup: 'usergroup',
      imagefile: file
    }
    uploadFacePy(formData).then(res => {
      Notify({
        type: 'primary',
        message: res.data.msg
      });
    }).catch(err => {
      console.log('失败回调', err)
    })

  },
  delete() {
    this.setData({
      fileList: []
    });
  }
})